package com.mbv5.xml;

import java.io.InputStream;

/**
 * Created by qiao on 2017/1/7.
 */
public class ResourceLoader {
    
    public InputStream build(String resource) {
        ClassLoader[] loaders = getClassLoader();
        for (ClassLoader cl : loaders) {
            if (null != cl) {
                // try to find the resource as passed
                InputStream returnValue = cl.getResourceAsStream(resource);
                // now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resource
                if (null == returnValue) {
                    returnValue = cl.getResourceAsStream("/" + resource);
                }
                if (null != returnValue) {
                    return returnValue;
                }
            }
        }
        return null;
    }

    private ClassLoader[] getClassLoader() {
        return new ClassLoader[]{
                null,
                Thread.currentThread().getContextClassLoader(),
                getClass().getClassLoader(),
                ClassLoader.getSystemClassLoader()
        };
    }

}
